<template>
	<view>
		<view class="header-wrapper">
			<image class="header-wrapper_bg" :src="bgImg" mode="scaleToFill"></image>
			<view class="status-bar" :style="{ height: statusBarH+'px' }"></view>
			<view class="navbar-wrapper" :style="{ height: navbar.height+'px' }">
				<view class="navbar-box" :style="[navbarBoxStyle]">
					<view class="navbar-box_left">
						<slot name="left">
							<view class="navbar-back" :style="[backIconStyle]" v-if="back" @click="handleBack">
								<u-icon name="arrow-left" color="rgba(0,0,0,0.9)" size="34"></u-icon>
							</view>
							<image class="navbar-logo" :src="logoLeft" mode="heightFix" v-else-if="logoLeft"></image>
						</slot>
					</view>
					<view class="navbar-box_center">
						<slot name="center">
							<view class="navbar-title" v-if="title">{{title}}</view>
						</slot>
					</view>
				</view>
			</view>
		</view>
		<view class="header-place" :style="{ height: headerH+'px' }">
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			title: {
				type: String
			},
			back: {
				type: Boolean,
				default: false
			},
			bgImg: {
				type: String
			},
			logoLeft: {
				type: String
			}
		},

		data() {
			return {
				headerH: 0,
				statusBarH: 0,
				navbar: {
					width: 0,
					height: 0,
				},
				navbarBoxStyle: {
					left: '32rpx',
					right: '32rpx',
					top: 0,
					bottom: 0,
				},
				backIconStyle: {
					width: 0,
					height: 0,
					top: 0,
				},
			};
		},

		created() {
			let statusBarH = uni.getSystemInfoSync().statusBarHeight;
			this.statusBarH = statusBarH
			let wxMenuBtn = uni.getMenuButtonBoundingClientRect()
			let wxMenuBtnMarginLR = 375 - wxMenuBtn.right
			let wxMenuBtnMarginTD = wxMenuBtn.top - statusBarH
			this.navbar = {
				width: (wxMenuBtn.left - wxMenuBtnMarginLR * 2),
				height: (wxMenuBtn.height + (wxMenuBtn.top - statusBarH) * 2),
			}
			this.headerH = this.statusBarH + this.navbar.height
			this.navbarBoxStyle = {
				left: '32rpx',
				right: '32rpx',
				top: wxMenuBtnMarginTD + 'px',
				bottom: wxMenuBtnMarginTD + 'px',
			}
			this.backIconStyle = {
				width: wxMenuBtn.height + 'px',
				height: wxMenuBtn.height + 'px',
				top: wxMenuBtnMarginTD + 'px',
			}
		},
		methods: {
			handleBack() {
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped lang="scss">
	.header-wrapper {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 999;

		&_bg {
			position: absolute;
			top: 0;
			left: 0;
			display: block;
			width: 100%;
			height: 100%;
			z-index: -1;
		}
	}

	.navbar-wrapper {
		position: relative;

		.navbar-box {
			position: absolute;
			display: flex;
			align-items: center;
			justify-content: center;

			&_left,
			&_right {
				position: absolute;
				top: 0;
				bottom: 0;
				align-items: center;
			}

			&_left {
				left: 0;

				.navbar-back {
					position: absolute;
					left: 0;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}

			&_right {
				right: 0;
			}

			&_center {
				text-align: center;

				.navbar-title {
					font-size: 34rpx;
					width: 120px;
					overflow: hidden;
					white-space: nowrap;
					text-align: center;
					text-overflow: ellipsis;
				}
			}

			.navbar-logo {
				display: block;
				height: 100%;
			}
		}
	}
</style>
